#!/usr/bin/env python
import sys, socket, time, threading

loglock = threading.Lock()
def log(msg):
  loglock.acquire()
  try:
    print('[%s]: \n%s\n' % (time.ctime(), msg.strip()))
    sys.stdout.flush()
  finally:
    loglock.release()

class PipeThread(threading.Thread):
  def __init__(self, source, target):
    threading.Thread.__init__(self)
    self.source = source
    self.target = target

  def run(self):
    while True:
      try:
        data = self.source.recv(1024)
        # log(data)# 序列化比较耗时间
        if not data: break
        self.target.send(data)
      except:
        break
    log('PipeThread done')

class Frps(threading.Thread):
  def __init__(self, port, targetport):
    threading.Thread.__init__(self)
    self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.sock.bind(('0.0.0.0', port))
    self.sock.listen(10)

    self.targetSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.targetSock.bind(('0.0.0.0', targetport))
    self.targetSock.listen(10)
  def run(self):
    while True:
      client_fd, client_addr = self.sock.accept()
      target_client_fd, target_client_addr = self.sock.accept()
      log('new connect')
      # two direct pipe
      PipeThread(target_client_fd, client_fd).start()
      PipeThread(client_fd, target_client_fd).start()


if __name__ == '__main__':
  print('Starting')
  try:
    port = int(sys.argv[1])
    try: targetport = int(sys.argv[2])
    except IndexError: targetport = port
  except (ValueError, IndexError):
    print ('Usage: %s port targethost [targetport]' % sys.argv[0])
    sys.exit(1)

  #sys.stdout = open('forwaring.log', 'w')
  Frps(port, targetport).start()
